//
//  YCOriginalView.swift
//  weiBoSwift
//
//  Created by 刘胤辰 on 16/7/24.
//  Copyright © 2016年 it.com. All rights reserved.
//

import UIKit
import SnapKit

class YCOriginalView: UIView {

    override init(frame: CGRect) {
        
        super.init(frame: frame)
        //添加
        self.backgroundColor=UIColor.whiteColor()
        self.addSubview(headImage)
        self.addSubview(nameLabel)
        self.addSubview(timeLabel)
        self.addSubview(rankImage)
        self.addSubview(textLabel)
        self.addSubview(avatarImageView)
        self.addSubview(imageColView)
        //设置约束
        headImage.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(self.snp_top).offset(marginW)
            make.leading.equalTo(self.snp_leading).offset(marginW)
            make.size.equalTo(CGSizeMake(35, 35))
        }
        nameLabel.snp_makeConstraints { (make) -> Void in
            
            make.leading.equalTo(headImage.snp_trailing).offset(marginW)
            make.top.equalTo(headImage.snp_top)
        }
        timeLabel.snp_makeConstraints { (make) -> Void in
            
            make.leading.equalTo(nameLabel.snp_leading)
            make.bottom.equalTo(headImage.snp_bottom)
        }
        rankImage.snp_makeConstraints { (make) -> Void in
            
            make.leading.equalTo(nameLabel.snp_trailing).offset(marginW)
            make.top.equalTo(headImage.snp_top)
        }
        textLabel.snp_makeConstraints { (make) -> Void in
            
            make.leading.equalTo(headImage.snp_leading)
            make.top.equalTo(headImage.snp_bottom).offset(marginW)
        }
        avatarImageView.snp_makeConstraints { (make) -> Void in
            
            make.centerX.equalTo(headImage.snp_trailing)
            make.centerY.equalTo(headImage.snp_bottom)
        }
        //配图
        imageColView.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(textLabel.snp_bottom).offset(10)
            
            make.leading.equalTo(self).offset(50)
        }
        self.snp_makeConstraints { (make) -> Void in
            
            self.imageConstraint = make.bottom.equalTo(imageColView.snp_bottom).offset(marginW).constraint
        }
    }
    
    // MARK: -- 懒加载
    //数据源
    var status : YCHomeStatus?{
        didSet{
            //设置等级图片
            rankImage.image=getMemberImage(status?.user?.mbrank)
            //设置头像
            headImage.yc_webImage(status?.user?.profile_image_url , imageName: "avatar_default")
            
            //设置标题
            nameLabel.text=status?.user?.name
            //设置时间
            timeLabel.text=status?.created_at
            //设置正文
            textLabel.text=status?.text
            //认证logo
            avatarImageView.image=getVerifiedImage(status?.user?.verified)
            //设置配图的大小
            //解决cell重用的问题
            //卸载约束
            self.imageConstraint?.uninstall()
            
            if status?.pic_urls?.count<=0 {
                
                self.snp_makeConstraints(closure: { (make) -> Void in
                    self.imageConstraint = make.bottom.equalTo(textLabel.snp_bottom).offset(marginW).constraint
                })
                imageColView.hidden=true
                
            }else{
               
                //赋值
                imageColView.imageLists=status?.pic_urls
                //重新约束，并记录
                self.snp_makeConstraints(closure: { (make) -> Void in
                    self.imageConstraint = make.bottom.equalTo(imageColView.snp_bottom).offset(marginW).constraint
                })
                imageColView.hidden=false
            }
        }
    }
    //头像
    private lazy var headImage:UIImageView=UIImageView()

    //等级
    private lazy var rankImage:UIImageView={
        
       let img=UIImageView(imageName: "common_icon_membership")
        img.sizeToFit()
        
        return img
    }()
    //标题
    private lazy var nameLabel:UILabel = UILabel(text: "", textColor: UIColor.blackColor(), font: 15)
    //时间
    private lazy var timeLabel:UILabel = UILabel(text: "", textColor: UIColor.greenColor(), font: 10)
    //正文内容
    private lazy var textLabel:UILabel = {
        
        let lab=UILabel(text: "", textColor: UIColor.blackColor(), font: 15)
        lab.numberOfLines=0
        lab.preferredMaxLayoutWidth=SCREENW-20
        
        return lab
    }()
    //认证logo
    private lazy var avatarImageView: UIImageView = {
        
        let image=UIImageView(imageName: "avatar_vgirl")
        
        image.sizeToFit()
        
        return image
    }()

    
    //处理等级图片
    func getMemberImage(mbrank:Int?)->UIImage?{
        
        guard let i=mbrank else{
            return nil
        }
        if i < 0 && i>7{
            return UIImage(named: "common_icon_membership")
        }else{
            
            return UIImage(named: "common_icon_membership_level\(i)")
        }
        
    }
    //创建配图
    var imageColView:YCHomeImageColView=YCHomeImageColView()
    //记录约束
    private var imageConstraint:Constraint?
    //处理认证图片
    func getVerifiedImage(num:Int?)->UIImage?{
        
        guard let i=num else{
            
            return nil
        }
        switch (i){
        case 1:
            return UIImage(named: "avatar_vip")
        case 2,3,4:
            return UIImage(named: "avatar_enterprise_vip")
        case 220:
            return UIImage(named: "avatar_grassroot")
        default:
            return UIImage(named: "avatar_vgirl")
        }
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}
